package com.xlsx;

import cn.hutool.core.io.FileUtil;
import com.xlsx.utils.DownTemp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * 创建一个JFrame容器
 */
public class JFrame {

    private javax.swing.JFrame frame;
    private JTextField tableNameField;
    private File selectedFile;

    public void FileUploadWithSpecificFileType() {
        frame = new javax.swing.JFrame("xlsx转建表语句");
        frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        String path = DownTemp.path();
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.setBackground(new Color(240, 240, 240));

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(5, 5, 5, 5);

        Font font = new Font("宋体", Font.BOLD, 14);

        JLabel tableNameLabel = new JLabel("生成文件名:");
        tableNameLabel.setFont(font);
        constraints.gridx = 0;
        constraints.gridy = 0;
        panel.add(tableNameLabel, constraints);

        tableNameField = new JTextField(20);
        tableNameField.setFont(font);
        constraints.gridx = 1;
        constraints.gridy = 0;
        panel.add(tableNameField, constraints);

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

        JButton downloadButton = new JButton("模板下载");
        downloadButton.setFont(font);
        downloadButton.setBackground(new Color(0, 128, 255));
        downloadButton.setForeground(Color.WHITE);
        downloadButton.setBorder(BorderFactory.createRaisedBevelBorder());
        downloadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                DownTemp downTemp = new DownTemp();
                try {
                    downTemp.export();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
        });

        buttonPanel.add(downloadButton);

        JButton uploadButton = new JButton("xlsx文件");
        uploadButton.setFont(font);
        uploadButton.setBackground(new Color(0, 128, 255));
        uploadButton.setForeground(Color.WHITE);
        uploadButton.setBorder(BorderFactory.createRaisedBevelBorder());
        uploadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser(path);
                fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                fileChooser.setMultiSelectionEnabled(false);
                fileChooser.setDialogTitle("默认打开桌面路径、请选择xlsx文件");
                fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        String name = f.getName().toLowerCase();
                        return f.isDirectory() || name.endsWith(".xls") || name.endsWith(".xlsx");
                    }

                    @Override
                    public String getDescription() {
                        return "Excel 文件 (*.xls, *.xlsx)";
                    }
                });

                int returnValue = fileChooser.showOpenDialog(frame);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    selectedFile = fileChooser.getSelectedFile();
                }
            }
        });

        buttonPanel.add(uploadButton);

        JButton confirmButton = new JButton("导出大写");
        confirmButton.setFont(font);
        confirmButton.setBackground(new Color(0, 128, 255));
        confirmButton.setForeground(Color.WHITE);
        confirmButton.setBorder(BorderFactory.createRaisedBevelBorder());
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String tableName = tableNameField.getText();
                if ( selectedFile == null) {
                    JOptionPane.showMessageDialog(frame, "文件不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
                } else {
                    createSqlPlus createSqlPlus = new createSqlPlus();
                    try {
                        System.out.println("tableName:"+tableName);
                        System.out.println("selectedFile:"+selectedFile.getAbsolutePath());
                        createSqlPlus.createSql(tableName,selectedFile.getAbsolutePath(),1);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(frame, "请去"+path+"路径下查看表名: " + tableName);
                }
            }
        });

        buttonPanel.add(confirmButton);

        JButton confirmButton2 = new JButton("导出小写");
        confirmButton2.setFont(font);
        confirmButton2.setBackground(new Color(0, 128, 255));
        confirmButton2.setForeground(Color.WHITE);
        confirmButton2.setBorder(BorderFactory.createRaisedBevelBorder());
        confirmButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String tableName = tableNameField.getText();
                if ( selectedFile == null) {
                    JOptionPane.showMessageDialog(frame, "文件不能为空！", "错误", JOptionPane.ERROR_MESSAGE);
                } else {
                    createSqlPlus createSqlPlus = new createSqlPlus();
                    try {
                        System.out.println("tableName:"+tableName);
                        System.out.println("selectedFile:"+selectedFile.getAbsolutePath());
                        createSqlPlus.createSql(tableName,selectedFile.getAbsolutePath(),2);
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(frame, "请去"+path+"路径下查看表名: " + tableName);
                }
            }
        });

        buttonPanel.add(confirmButton2);



        frame.add(panel, BorderLayout.CENTER);
        frame.add(buttonPanel, BorderLayout.SOUTH);

        frame.setSize(new Dimension(400, 200));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
